from flask_restful import Resource
from app import api,auth,celery,mail
from flask import jsonify,g,request
from app.models import *
from utils import *
from tasks import *

class sendmailAPI(Resource):
    decorators = [auth.login_required]
    def post(self):
        subject = request.json.get('subject','') 
        sendmail = request.json.get('sendmail')
        body = request.json.get('body')
        if not sendmail:
            return jsonify({'code':10003,'msg':'please input senndmail'})
        if not body:
            return jsonify({'code':10003,'msg':'please input body'})
        email_data = {
            'subject': subject,
            'to': sendmail,
            'body': body
          }
        task=send_async_email.delay(email_data)
        return jsonify({'code':200,'task_id':task.id,'msg':'添加队列成功'}) 


api.add_resource(sendmailAPI, '/devops/api/v1.0/sendmail', endpoint = 'sendmail')
